package com.yintao.ytframe.base.juc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yintao
 * @desc 不同线程顺序交替打印
 * @package com.yintao.ytframe.base.juc
 * @date 2021/2/822:12
 */
public class OrderPrint {

    public static AtomicInteger ai = new AtomicInteger(0);
    public static final int MAX_SYC_VALUE = 20;

    public static void main(String[] args) {

        OrderPrint atomic_ABC = new OrderPrint();
        ExecutorService service = Executors.newFixedThreadPool(2);

        service.execute(atomic_ABC.new RunnableA());
        service.execute(atomic_ABC.new RunnableB());

    }

    private class RunnableB implements Runnable {
        public void run() {
            while (ai.get() < MAX_SYC_VALUE) {
                if (ai.get() % 2 == 1) {
                    System.out.println("B");
                    ai.getAndIncrement();
                }
            }

        }
    }

    private class RunnableA implements Runnable {
        public void run() {
            while (ai.get() < MAX_SYC_VALUE) {
                if (ai.get() % 2 == 0) {
                    System.out.println("A");
                    ai.getAndIncrement();
                }
            }

        }
    }


}
